package User;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.sql.*;

public class Register extends JFrame {
    JPanel root;
    JLabel usernameLabel, passwordLabel, genderLabel, phoneLabel, emailLabel;
    JTextField usernamefield,  genderfield, phonefield, emailfield;
    JPasswordField passwordfield;
    JButton registerButton,returnButton;
    private static String registeredUsername = "";

    private static final String URL = "jdbc:mysql://localhost:3306/bookmanagement";
    private static final String USER = "root";
    private static final String PASSWORD = "root";



    private boolean correctUsername(String username) {
        return username.length() >= 6 && username.length() <= 20;
    }

    private boolean correctPassword(String password) {
        HashSet<Character> charSet = new HashSet<>();
        for (char c : password.toCharArray()) {
            charSet.add(c);
            if (charSet.size() >= 3) {
                return true;
            }
            }
        return false;
    }

    public Register() {
        root = new JPanel();
        setContentPane(root);
        setLayout(null);
        usernameLabel = new JLabel("用户名：");
        usernameLabel.setBounds(52, 33, 54, 15);
        root.add(usernameLabel);
        usernamefield = new JTextField(12);
        usernamefield.setBounds(116, 30, 139, 21);
        root.add(usernamefield);
        passwordLabel = new JLabel("密 码：");       //定义标签对象
        passwordLabel.setBounds(52, 74, 54, 15);
        root.add(passwordLabel);
        passwordfield = new JPasswordField(12);
        passwordfield.setBounds(116, 71, 139, 21);
        root.add(passwordfield);
        genderLabel = new JLabel("性别：");
        genderLabel.setBounds(52, 115, 54, 15);
        root.add(genderLabel);
        genderfield = new JTextField(12);
        genderfield.setBounds(116, 112, 139, 21);
        root.add(genderfield);
        phoneLabel = new JLabel("电话：");
        phoneLabel.setBounds(52, 156, 54, 15);
        root.add(phoneLabel);
        phonefield = new JTextField(12);
        phonefield.setBounds(116, 153, 139, 21);
        root.add(phonefield);
        emailLabel = new JLabel("邮箱：");
        emailLabel.setBounds(52, 197, 54, 15);
        root.add(emailLabel);
        emailfield = new JTextField(12);
        emailfield.setBounds(116, 194, 139, 21);
        root.add(emailfield);
        registerButton = new JButton("注册");
        registerButton.setBounds(50,250,100,36);
        root.add(registerButton);
        returnButton = new JButton("返回");
        returnButton.setBounds(200,250,100,36);
        root.add(returnButton);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setBounds(400,300,340,400);
        setVisible(true);

        registerButton.addActionListener(e-> {
            String username = usernamefield.getText();
            String password = passwordfield.getText();
            String gender = genderfield.getText();
            String phone = phonefield.getText();
            String email = emailfield.getText();

            if (correctUsername(username) && correctPassword(password)) {
                try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) {
                    String sql = "INSERT INTO usercreate (username, password, gender, phone, email) VALUES (?, ?, ?, ?, ?)";
                    try (PreparedStatement statement = connection.prepareStatement(sql)) {
                        statement.setString(1, username);
                        statement.setString(2, password);
                        statement.setString(3, gender);
                        statement.setString(4, phone);
                        statement.setString(5, email);

                        int row = statement.executeUpdate();
                        if (row > 0) {
                            JOptionPane.showMessageDialog(this, "注册成功！");
                            Userlogin userlogin1 = new Userlogin();
                            userlogin1.setVisible(true);
                            dispose();
                        } else {
                            JOptionPane.showMessageDialog(this, "注册失败，未影响任何行。", "错误", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                } catch (SQLException ex) {
                    JOptionPane.showMessageDialog(this, "数据库错误: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(this, "用户名或密码无效", "错误", JOptionPane.ERROR_MESSAGE);
            }
        });

        returnButton.addActionListener(e-> {
            Userlogin userlogin2 = new Userlogin();
            userlogin2.setVisible(true);
            this.dispose();
        });

    }

    public static void main(String[] args) {
        new Register();
    }
}